<template>
  <el-dialog :title="option.title" :visible.sync="option.isShow" :close-on-click-modal="false" @open="openDialog"
    @close="closeDialog('ruleForm')">
    <el-row :gutter="10">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="130px" class="demo-ruleForm">
        <el-col :span="16">
          <el-form-item label="案例编号" prop="CASENO">
            <el-input size="mini" placeholder="请输入案例编号" disabled v-model="ruleForm.CASENO"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="16">
          <el-form-item label="文件类型" prop="FILETYPE">
            <el-input size="mini" placeholder="请输入文件类型" disabled v-model="ruleForm.FILETYPE"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="16">
          <el-form-item label="文件路径" prop="FILEPATH">
            <el-input size="mini" placeholder="请输入文件路径" disabled v-model="ruleForm.FILEPATH"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="16">
          <el-form-item label="文件名" prop="FILENAME">
            <el-input size="mini" placeholder="请输入文件名" disabled v-model="ruleForm.FILENAME"></el-input>
          </el-form-item>
        </el-col>
        <!-- <el-col :span="12">
          <el-form-item label="学习任务序号" prop="TASKSERINO">
            <el-input size="small" placeholder="请输入案例名称" v-model="ruleForm.TASKSERINO"></el-input>
          </el-form-item>
        </el-col>-->

        <!-- <el-col :span="16">
          <el-form-item label="任务分配人员" prop="ALLOCATER">
            <el-input size="mini" placeholder="请输入任务分配人员" v-model="ruleForm.ALLOCATER"></el-input>
          </el-form-item>
        </el-col> -->
        <el-col :span="16">
          <el-form-item label="任务开始日期" prop="TASKBEGINDATE">
            <el-date-picker size="mini" style="width:100%;" v-model="ruleForm.TASKBEGINDATE" type="date"
              value-format="yyyyMMdd" placeholder="任务开始日期" :picker-options="pickerOptionsStart" @change="changeStart">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="16">
          <el-form-item label="任务结束日期" prop="TASKENDDATE">
            <el-date-picker style="width:100%;" size="mini" v-model="ruleForm.TASKENDDATE" type="date"
              value-format="yyyyMMdd" placeholder="任务结束日期" :picker-options="pickerOptionsEnd" @change="changeEnd">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="16">
          <el-form-item label="坐席ID" prop="AGENTID">
            <el-select v-model="ruleForm.AGENTID" size="mini" multiple placeholder="请输入坐席ID" @change="change">
              <el-option v-for="(item,index) in agentIdOptions" :key="index" :label="item.val" :value="item.val">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <!-- <el-col :span="16">
          <el-form-item label="分配日期" prop="ALLOCATDATE">
            <el-date-picker
              size="mini"
              style="width:100%;"
              v-model="ruleForm.ALLOCATDATE"
              type="date"
              value-format="yyyyMMdd"
              placeholder="分配日期"
            ></el-date-picker>
          </el-form-item>
        </el-col> -->
        <el-col :span="24">
          <el-form-item>
            <el-button type="primary" size="mini" @click="submitForm('ruleForm')">保存</el-button>
            <el-button @click="resetForm('ruleForm')" size="mini">重置</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-row>
  </el-dialog>
</template>

<script>
  import request from "@/common/utils/requestQC";
  import getGlobalParams from "@/common/utils/getGlobalParams";
  import addData from "@/message/QCManage/caseManage/caseTaskAdd";
  import toolBox from "@/common/utils/toolBox";
  // 坐席维护接口
  import agentManage from "@/message/seat-manage/SeatInfoManage/agentManage";
  import requestYg from "@/common/utils/requestYg";

  export default {
    props: {
      option: Object
    },
    computed: {
      isShow: {
        get: function () {
          return this.option.isShow;
        },
        set: function (newValue) {
          this.option.isShow = newValue;
        }
      }
    },
    created() {
      this.ruleForm.TASKBEGINDATE = toolBox.getCurrentMonthFirstDay();
      this.changeStart();
      this.ruleForm.TASKENDDATE = toolBox.getCurrentMonthLastDay();
      this.changeEnd();
    },
    mounted() {
      this.agentQuery();
    },
    data() {
      return {
        ruleForm: {
          CASENO: "",
          FILENAME: '',
          FILETYPE: '',
          FILEPATH: '',
          TASKSERINO: "",
          TASKBEGINDATE: "",
          TASKENDDATE: "",
          AGENTID: "",
          ALLOCATER: "",
          ALLOCATDATE: "",
          loading: false
        },
        agentid: '',
        AGENTID: '',
        AGENTROLE: '',
        STAFSTS: '',
        AGENTDEP: '',
        AGENTCLASS: '',
        AGENTTEAM: '',
        agentIdOptions: [],
        tableData: [],
        pickerOptionsStart: {
          disabledDate: time => {
            // return time.getTime() < Date.now()
          }
        },
        pickerOptionsEnd: {
          disabledDate: time => {
            // return time.getTime() < Date.now()
          }
        },
        rules: {
          TASKBEGINDATE: [{
            required: true,
            message: "请输入任务开始日期",
            trigger: "change"
          }],
          TASKENDDATE: [{
            required: true,
            message: "请输入任务结束日期",
            trigger: "change"
          }],
          AGENTID: [{
            required: true,
            message: "请输入坐席ID",
            trigger: "change"
          }]
        }
      };
    },

    methods: {
      openDialog() {
        if (this.$refs["ruleForm"] != undefined) {
          this.$refs["ruleForm"].clearValidate();
        }

        this.ruleForm.CASENO = this.option.row.CASENO;
        this.ruleForm.FILEPATH = this.option.row.FILEPATH;
        this.ruleForm.FILENAME = this.option.row.FILENAME;
        this.ruleForm.FILETYPE = this.option.row.FILETYPE;

      },
      closeDialog(formName) {
        if (this.$refs[formName] != undefined) {
          this.$refs[formName].clearValidate();
        }
        // this.$refs[formName].clearValidate();
        this.$refs[formName].resetFields();
      },
      //保存
      submitForm(formName) {
        this.$refs[formName].validate(async valid => {
          if (valid) {
            let resBody;
            resBody = new addData();
            resBody.data.CASENO = this.ruleForm.CASENO;
            resBody.data.TASKSERINO = "";
            resBody.data.TASKBEGINDATE = this.ruleForm.TASKBEGINDATE;
            resBody.data.TASKENDDATE = this.ruleForm.TASKENDDATE;
            if (this.ruleForm.AGENTID.length > 1) {
              for (let i = 0; i < this.ruleForm.AGENTID.length; i++) {
                this.agentid += this.ruleForm.AGENTID[i] + '|';
                if (i == this.ruleForm.AGENTID.length - 1) {
                  this.agentid += this.ruleForm.AGENTID[i];
                }
              }
              resBody.data.AGENTID = this.agentid;
            } else {
              resBody.data.AGENTID = this.ruleForm.AGENTID[0];
            }
            resBody.data.ALLOCATER = this.ruleForm.ALLOCATER;
            resBody.data.ALLOCATDATE = this.ruleForm.ALLOCATDATE;
            resBody.data.FILEPATH = this.ruleForm.FILEPATH;
            resBody.data.FILETYPE = this.ruleForm.FILETYPE;
            resBody.data.FILENAME= this.ruleForm.FILENAME;
            let response = await request(resBody);
            if (response.SYS_HEAD.ReturnCode == "000000") {
              this.$message({
                type: "success",
                message: "新增案例学习任务成功！"
              });

              this.isShow = false;
              this.$emit("refreshTable");
            } else {
              this.$message({
                type: "error",
                message: "请求失败，错误码:" +
                  response.SYS_HEAD.ReturnCode +
                  "，错误信息：" +
                  response.SYS_HEAD.ReturnMessage
              });
            }
          } else {
            console.log("error submit!!");
            return false;
          }
        });
      },
      changeStart() {
        this.pickerOptionsEnd = Object.assign({}, this.pickerOptionsEnd, {
          disabledDate: time => {
            if (this.ruleForm.TASKBEGINDATE) {
              return (
                time.getTime() <
                Date.parse(
                  this.ruleForm.TASKBEGINDATE.slice(0, 4) +
                  "-" +
                  this.ruleForm.TASKBEGINDATE.slice(4, 6) +
                  "-" +
                  this.ruleForm.TASKBEGINDATE.slice(6, 8)
                ) -
                8.64e7
              );
            } else {
              // return time.getTime() < Date.now();
            }
          }
        });
      },
      changeEnd() {
        this.pickerOptionsStart = Object.assign({}, this.pickerOptionsStart, {
          disabledDate: time => {
            if (this.ruleForm.TASKENDDATE) {
              return (
                time.getTime() >
                Date.parse(
                  this.ruleForm.TASKENDDATE.slice(0, 4) +
                  "-" +
                  this.ruleForm.TASKENDDATE.slice(4, 6) +
                  "-" +
                  this.ruleForm.TASKENDDATE.slice(6, 8)
                )
              );
            } else {
              // return time.getTime() < Date.now();
            }
          }
        });
      },
      resetForm(formName) {
        if (this.$refs[formName] != undefined) {
          this.$refs[formName].clearValidate();
          this.$refs[formName].resetFields();
        }
      },
      // 查询坐席
      async agentQuery() {
        this.loading = false;
        let resBody = new agentManage();
        resBody.data.OPERATION = '4'
        resBody.data.AGENTID = this.AGENTID;
        resBody.data.AGENTROLE = this.AGENTROLE;
        resBody.data.STAFSTS = this.STAFSTS;
        resBody.data.AGENTDEP = this.AGENTDEP;
        resBody.data.AGENTCLASS = this.AGENTCLASS;
        resBody.data.AGENTTEAM = this.AGENTTEAM;
        resBody.data.PAGEFLAG = 4;
        resBody.data.PAGEROWNUM = 10;
        resBody.data.CURRPAGE = 1;
        requestYg(resBody)
          .then(response => {
            if (response.SYS_HEAD.ReturnCode === "000000") {
              this.tableData = response.RSP_BODY.struct;
              for (let i = 0; i < this.tableData.length; i++) {
                this.agentIdOptions.push({
                  'code': i,
                  'val': this.tableData[i].AGENTID + ',' + this.tableData[i].AGENTNAME
                })
              }
            } else {
              this.$message({
                type: "error",
                message: "请求失败，错误码: " +
                  response.SYS_HEAD.ReturnCode +
                  ", 错误信息: " + response.SYS_HEAD.ReturnMessage
              });
              this.tableData = [];
            }
            this.loading = false;
          })
          .catch(error => {
            this.loading = false;
          });
      },
      change() {
        console.log(JSON.stringify(this.ruleForm.AGENTID))
      }
    }
  };

</script>

<style rel="stylesheet/scss" lang="scss" scoped>
</style>
